package co.edu.unal.franquicias.mastercard.auth;

import com.sun.jersey.api.view.Viewable;

import javax.ws.rs.GET;
import javax.ws.rs.Path;
import javax.ws.rs.PathParam;
import javax.ws.rs.Produces;
import javax.ws.rs.core.MediaType;
import javax.ws.rs.core.Response;
import java.util.Collection;
import javax.ws.rs.FormParam;
import javax.ws.rs.POST;

/**
 * List of all user statuses.
 * Sub-resource available at the following path:
 * <pre>/{user}/statuses/</pre>
 */
public class StatusesResource extends ResourceBase {

    StatusesResource(UserResource parent) {
        super(parent);
    }

    /**
     * HTML representation of the list of all user statuses.
     *
     * @return HTML representation of the list of all user statuses
     */
    @GET
    @Produces("text/html;qs=2")
    public Viewable getHtml() {
        return new Viewable("/statuses", getXml());
    }

    /**
     * XML/JSON representation of the list of all user statuses.
     *
     * @return XML/JSON representation of the list of all statuses
     */
    @GET
    @Produces({"application/xml", "application/json"})
    public Collection<Status> getXml() {
        checkUser();
        return DataProvider.getStatuses(user);
    }

    /**
     * Creates a new status based on the form parameters passed in the request.
     *
     * @return redirect to the list of notes
     */
    @POST
    public Response create(@FormParam("text") String text) {
        checkUser();
        checkWrite();
        DataProvider.createStatus(user, text);
        return Response.seeOther(uriInfo.getRequestUri()).build();
    }

    /**
     * Retrieves a form for creating a new status.
     *
     * @return HTML page with the form
     */
    @Path("new")
    @GET
    @Produces(MediaType.TEXT_HTML)
    public Viewable getForm() {
        checkUser();
        return new Viewable("/newstatus", null);
    }

    /**
     * Sub-resource locator for {@link StatusResource}.
     *
     * @param statusId ID of the status
     * @return status resource corresponding to the status ID
     */
    @Path("{statusId: [0-9]+}")
    public StatusResource getStatusResource(@PathParam("statusId") int statusId) {
        return new StatusResource(statusId, this);
    }
}
